package com.sinoglobal.dumping.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.base.Dumpling_ShareAbstractActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.view.Dumpling_CancelSendDumplingsDialog;
import com.sinoglobal.dumping.view.Dumpling_QQWXShare;
import com.umeng.socialize.bean.SHARE_MEDIA;

public class Dumpling_DumplingsShareActivity extends Dumpling_ShareAbstractActivity {

    private String shareUrl;
    private String shareContent;
    private String shareTitle;
    private String sharePicUrl;
    private Button sendDumplings;
    private Dumpling_CancelSendDumplingsDialog cancelDialog;
    private String pannikinId;
    private RelativeLayout relativeLayout;
    private Dumpling_QQWXShare qqwxShare;
    private boolean goMainActivity=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dumpling_activity_dumplings_complete);
        init();
        pannikinId = getIntent().getStringExtra("paySuccessId");
        mTemplateRightImg.setVisibility(View.GONE);
        mTemplateRightText.setVisibility(View.GONE);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_dumpling_send_dumplings);
        findViewById(R.id.title_right_btn_share).setVisibility(View.GONE);
        mTemplateTitleText.setText("包饺子");
        sendDumplings = (Button) findViewById(R.id.btn_send_dumplings);
        sendDumplings.setOnClickListener(this);
        mTemplateLeftImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }
    /**
     * 获取分享信息
     */
    private void init() {
        pannikinId = getIntent().getStringExtra("paySuccessId");
        if (Dumpling_SinoConfig.sMakeShare == null) {
            showToast("访问网络失败，请稍后再试");
            goIntent(Dumpling_MainActivity.class);
        } else {
            shareUrl = Dumpling_SinoConfig.sMakeShare.getResultList().getUrl() + "?name=" + Dumpling_SinoConstans.USER_NAME + "&productCode=" + Dumpling_SinoConstans.PRODUCT_CODE + "&pannikin_id=" + pannikinId;
            shareContent = Dumpling_SinoConfig.sMakeShare.getResultList().getContent();
            shareTitle = Dumpling_SinoConfig.sMakeShare.getResultList().getTitle();
            sharePicUrl = Dumpling_SinoConfig.sMakeShare.getResultList().getPicurl();
            if (!shareUrl.contains("http://")){
                shareUrl="http://"+shareUrl;
            }
            qqwxShare = new Dumpling_QQWXShare(Dumpling_DumplingsShareActivity.this);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showDialog() {
        if (cancelDialog == null) {
            cancelDialog = new Dumpling_CancelSendDumplingsDialog(this);
        }
        if (!cancelDialog.isShowing()) {
            cancelDialog.show();
        }
    }

    @Override
    public void onAntiViolence(View view) {
        if (view.getId() == R.id.btn_send_dumplings) {
            if (Dumpling_SinoConfig.sMakeShare == null) {
                showToast("网络不好，请稍后再试");
                Dumpling_HttpMethodSet.getShareContent(Dumpling_DumplingsShareActivity.this, Dumpling_MainActivity.TEMP_ID_MAKE, "", "", "", "");
            } else {
                qqwxShare.onShare(relativeLayout, new Dumpling_QQWXShare.Dumpling_ShareListener() {
                    @Override
                    public void qqShare() {
                        setShare(shareContent, shareTitle, SHARE_MEDIA.QQ, shareUrl, 0, sharePicUrl);
                        Dumpling_HttpMethodSet.changeStatus(Dumpling_DumplingsShareActivity.this, pannikinId);
                        qqwxShare.sharePopDismiss();
                    }

                    @Override
                    public void wxShare() {
                        setShare(shareContent, shareTitle,SHARE_MEDIA.WEIXIN,shareUrl, 0, sharePicUrl);
                        Dumpling_HttpMethodSet.changeStatus(Dumpling_DumplingsShareActivity.this,pannikinId);
                        qqwxShare.sharePopDismiss();
                    }
                });
                goMainActivity=true;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (goMainActivity){
            goIntent(Dumpling_MainActivity.class);
        }
    }

    /** 分享成功 */
    protected void shareSuccess(){
        goIntent(Dumpling_MainActivity.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        qqwxShare.sharePopDismiss();
    }
}
